import json
import time

from django.db.models import Q
from django.http import HttpResponseForbidden,JsonResponse
from django.shortcuts import render
# Create your views here.
from django.views import View
from moment.models import Moment
from users.models import User, Friend
from utails.pushfile import request_to

from .utails import getMomentPl, getImage



class UserMomentView(View):
    '动态'
    def get(self, request):
        userid = request.GET.get('userid', None)
        if userid is None:
            return HttpResponseForbidden('400')
        try:
            user = User.objects.get(id=userid)
        except:
            return HttpResponseForbidden('403')
        moments = user.m_master.all()
        content = []
        for i in moments:
            content.append({
                'userimg': i.user.default_image,
                'username': i.user.username,
                'data': i.data,
                'img_data': i.img_data,
                'datetime': i.create_time,
                'pl_data': getMomentPl(i),
                'images': getImage(i)
            })
        return JsonResponse(content, safe=False)


class MomentPlView():
    '''朋友圈评论'''
    def get(self, request):
        pass


    def post(self, request):
        pass


class MomentImageView(View):
    def post(self, request):
        img_data = request.FILES.get('img',None)
        moment = request.POST.get('moment',None)
        if img_data is None or moment is None:
            return HttpResponseForbidden('403')
        # time.sleep(1)
        res = request_to(img_data, '', 0, 0)

        try:
            moment = Moment.objects.get(id=moment)
            moment.m_img.create(
                image_src=res
            )
            # moment.img_data = img_data
            # moment.save()
        except Exception as e:
            return JsonResponse({'code':'400'})
        return JsonResponse({'code':'200'})


class MomentView(View):
    '''朋友圈数据'''
    def get(self, request):
        userid = request.GET.get('userid', None)

        if userid is None:
            return HttpResponseForbidden('400')
        try:
            user = User.objects.get(id=userid)
        except:
            return HttpResponseForbidden('400')
        # 获取朋友
        friends = Friend.objects.filter(Q(user_a=user) | Q(user_b=user), status=1).order_by('-update_time')
        f_users = [user]
        for i in friends:
            friend = i.user_b if user==i.user_a else i.user_a
            f_users.append(friend)
        # 朋友圈数据
        moment = Moment.objects.filter(user__in=f_users).order_by('-create_time')


        content = []
        for i in moment:
            content.append({
                'userimg': i.user.default_image,
                'username': i.user.username,
                'data': i.data,
                'img_data': i.img_data,
                'datetime': i.create_time,
                'pl_data': getMomentPl(i),
                'images':getImage(i)
            })
        return JsonResponse(content, safe=False)
    def post(self, request):
        jsondict = json.loads(request.body.decode())
        userid = jsondict.get('userid')
        data = jsondict.get('data')
        if not all([userid, data]):
            return HttpResponseForbidden('403')
        try:
            user = User.objects.get(id=userid)
            moment = user.m_master.create(
                data=data
            )
        except:
            return JsonResponse({'code': '400'})
        return JsonResponse({'code': '200', 'data': moment.id})

class WebIndex(View):
    def get(self, request):
        return render(request,'index.html')
        # return JsonResponse({'code': 200})